//
//  XMainPage.m
//  food
//
//  Created by XmaMini on 2019/5/9.
//  Copyright © 2019 XmaMini. All rights reserved.
//

#import "XMainPage.h"
#import "XRCarouselView.h"
#import "QDTabBarViewController.h"
#import "XIndexPage.h"
#import "ReadTool.h"
#import "XInfoPage.h"

@implementation XGridView

@end

@interface XMainPage ()<XRCarouselViewDelegate>
@property(nonatomic, strong) QMUIGridView *gridView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) XRCarouselView *topBanner;
@property (nonatomic, strong) UIButton *leftTopButton;
@property (nonatomic, strong) UIButton *letBottonButton;
@property (nonatomic, strong) UIButton *rightButton;
@property(nonatomic) CGFloat viewWidth;
@property(nonatomic) CGFloat posY;

//XRCarouselView.h
@end

@implementation XMainPage

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    NSDictionary *dict =[ReadTool readLocalFileWithName:@"config"];
    NSArray *arr = nil;

    if(dict!=nil){
        self.banner = [dict objectForKey:@"banner"];
    
        if(self.banner.count == 3){
            arr = @[
                    [UIImage imageNamed:[[self.banner objectAtIndex:0] valueForKey:@"banner"]],//网络图片
                    [UIImage imageNamed:[[self.banner objectAtIndex:1] valueForKey:@"banner"]],//本地图片，传image，不能传名称
                    [UIImage imageNamed:[[self.banner objectAtIndex:2] valueForKey:@"banner"]]//网络gif图片
                    ];
        }else if(self.banner.count == 4){
            arr = @[
                    [UIImage imageNamed:[[self.banner objectAtIndex:0] valueForKey:@"banner"]],//网络图片
                    [UIImage imageNamed:[[self.banner objectAtIndex:1] valueForKey:@"banner"]],//本地图片，传image，不能传名称
                    [UIImage imageNamed:[[self.banner objectAtIndex:2] valueForKey:@"banner"]],//网络gif图片
                    [UIImage imageNamed:[[self.banner objectAtIndex:3] valueForKey:@"banner"]]//网络gif图片
                    ];
        }else{
            arr = @[
                    [UIImage imageNamed:[[self.banner objectAtIndex:0] valueForKey:@"banner"]],//网络图片
                    [UIImage imageNamed:[[self.banner objectAtIndex:1] valueForKey:@"banner"]],//本地图片，传image，不能传名称
                    ];
        }
        self.config =[dict objectForKey:@"index"];
    }else{
        arr = @[
                [UIImage imageNamed:@"loading1.png"],//网络图片
                [UIImage imageNamed:@"loading2.png"],//本地图片，传image，不能传名称
                [UIImage imageNamed:@"loading3.png"]//网络gif图片
                //                     gifImageNamed(@"2.gif")//本地gif使用gifImageNamed(name)函数创建
                ];
    }
        
    self.viewWidth = self.view.bounds.size.width - QMUICMI.navBarAccessoryViewMarginLeft*3;
    self.posY = QMUICMI.tableViewGroupedSectionFooterContentInset.top;
    
    CGRect infoRect = CGRectMake(QMUICMI.navBarAccessoryViewMarginLeft, self.view.bounds.origin.y + QMUICMI.tableViewGroupedSectionFooterContentInset.top , self.viewWidth, self.view.bounds.size.height - QMUICMI.tableViewGroupedSectionFooterContentInset.top*2);
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:infoRect];
    [self.view addSubview:self.scrollView];
    

    _topBanner = [[XRCarouselView alloc] initWithFrame:CGRectMake(0, self.posY, self.viewWidth, 250)];
    _topBanner.delegate = self;
    self.posY += 250;

   
    
    
    
    
    _topBanner.delegate = self;
    
    //设置占位图片,须在设置图片数组之前设置,不设置则为默认占位图
    _topBanner.placeholderImage = [UIImage imageNamed:@"loading1.png"];
    _topBanner.imageArray = arr;
    //    _topBanner.describeArray = describeArray;
    //
    //
    //    //设置图片切换的方式
    _topBanner.changeMode = ChangeModeFade;
    
    _topBanner.pageOffset = CGPointMake(0, -5);
    
    _topBanner.placeholderImage = [UIImage imageNamed:@"placeholderImage.jpg"];
    
    [self.scrollView addSubview:self.topBanner];
    
    
    self.posY += 20;
    float buttonwidth = (self.viewWidth - QMUICMI.navBarAccessoryViewMarginLeft)*0.55;
    float buttonheight = (buttonwidth /415) * 250 ;
    
 
    self.leftTopButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.leftTopButton.frame = CGRectMake(0, self.posY, buttonwidth, buttonheight);
    [self.leftTopButton setBackgroundImage:[UIImage imageNamed:@"btn_lefttop.jpg"] forState:UIControlStateNormal];
    [self.scrollView addSubview:self.leftTopButton];
    
    buttonwidth = (self.viewWidth - QMUICMI.navBarAccessoryViewMarginLeft)*0.45;
    buttonheight = (buttonwidth/311) * 476;
    self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.rightButton.frame = CGRectMake(self.viewWidth-buttonwidth, self.posY, buttonwidth, buttonheight);
    [self.rightButton setBackgroundImage:[UIImage imageNamed:@"btn_mid.jpg"] forState:UIControlStateNormal];
    [self.scrollView addSubview:self.rightButton];
    
    self.posY += buttonheight;
    
    buttonwidth = (self.viewWidth - QMUICMI.navBarAccessoryViewMarginLeft)*0.55;
    buttonheight = (buttonwidth /415) * 250 ;
//
    
    self.letBottonButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.letBottonButton.frame = CGRectMake(0, self.posY-buttonheight, buttonwidth, buttonheight);
    [self.letBottonButton setBackgroundImage:[UIImage imageNamed:@"btn_letbottom.jpg"] forState:UIControlStateNormal];
    [self.scrollView addSubview:self.letBottonButton];
    
    
    [self.leftTopButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.leftTopButton.tag = 0;
    
    [self.letBottonButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.letBottonButton.tag = 1;
    
    [self.rightButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton.tag = 2;
    
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(QMUICMI.navBarAccessoryViewMarginLeft, self.posY, 200.0, 50.0)];
    title.text = @"热门菜谱";
    title.textColor = QMUICMI.tabBarBarTintColor;
    [self.scrollView addSubview:title];
    self.posY += 40;
    
    
    
    
    self.gridView = [[QMUIGridView alloc] init];
    self.gridView.backgroundColor = UIColorWhite;
    self.gridView.columnCount = 3;

    float gridheight = (self.viewWidth / (self.gridView.columnCount + 1));
    self.gridView.rowHeight = gridheight;
    self.gridView.separatorWidth = 10;
    self.gridView.separatorColor = UIColorWhite;
    self.gridView.separatorDashed = NO;
    [self.scrollView addSubview:self.gridView];
    
    
    
    int showCount = 6;
    
    
    self.gridView.frame = CGRectMake(QMUICMI.navBarAccessoryViewMarginLeft, self.posY, self.viewWidth, gridheight*showCount / self.gridView.columnCount +((self.gridView.columnCount % self.gridView.columnCount) == 0?0:1));
    
    self.posY += self.gridView.bounds.size.height;
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.scrollView.bounds), self.posY+20);
    
    
    
    
    if(self.config){
        int idxArr[10] = {-1};
        for(int i = 0;i<showCount;i++){
            bool bRet = true;
            do{
                bRet = false;
                NSUInteger r = arc4random_uniform(self.config.count);
                for(int l = 0;l<showCount;l++){
                    if(idxArr[l] == r){
                        bRet = true;
                        break;
                    }
                }
                idxArr[i] = r;
            }while(bRet);
            
        }
        
        //随机数
        for (int i = 0; i < showCount; i++) {
            XGridView * cell =[[[NSBundle mainBundle] loadNibNamed:@"XGridViewItem" owner:nil options:nil] firstObject];
            [self.gridView addSubview:cell];
            cell.bgButton.tag = idxArr[i];
            [cell.bgButton addTarget:self action:@selector(girlviewclick:) forControlEvents:UIControlEventTouchUpInside];
            NSDictionary *dict = [self.config objectAtIndex:idxArr[i]];
            [cell.bgButton setImage:[UIImage imageNamed:[dict valueForKey:@"img"]] forState:UIControlStateNormal];
            cell.title.text = [dict valueForKey:@"name"];
        }
        
    }
    
}


-(void)girlviewclick:(UIButton *)btn{
    XInfoPage *viewController = [[XInfoPage alloc] init];
    [viewController setConfig:[self.config objectAtIndex:btn.tag]];
    [self.navigationController pushViewController:viewController animated:YES];
}

-(void)btnAction:(UIButton *)btn{
    
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    QDTabBarViewController *rootViewController = window.rootViewController;
//    rootViewController.selectedIndex = 1;
//    tab是可以取到其每一个子组件的，tabBarController.viewControllers 取到的就是所有子控制器；
//    如 ：第一个id v0 = tabBarController.viewControllers[0]， 这时候你就可以在tabbarController里面操作子组件了，是传值，还是调用方法，都是妥妥的！
//    [v0 ...]; OR v0.xxx = xxxxx;
    
    XIndexPage *viewController = [[XIndexPage alloc] init];
    if(btn.tag == 0){
        [viewController setShowType:1];
        [self.navigationController pushViewController:viewController animated:YES]; 
    }else if(btn.tag == 1){
        [viewController setShowType:2];
        [self.navigationController pushViewController:viewController animated:YES];
    }else if(btn.tag == 2){ 
        [viewController setShowType:3];
        [self.navigationController pushViewController:viewController animated:YES];
    }
}

#pragma mark XRCarouselViewDelegate
- (void)carouselView:(XRCarouselView *)carouselView clickImageAtIndex:(NSInteger)index {
    if(self.banner){
//        NSLog(@"击了第%ld张图片 nam:%@",index,[[self.banner objectAtIndex:index] valueForKey:@"name"]);
//        
        NSDictionary *dict = nil;
        for(int i = 0;i<self.config.count;i++){
            dict = [self.config objectAtIndex:i];
            if([[dict valueForKey:@"name"] isEqual:[[self.banner objectAtIndex:index] valueForKey:@"name"]]){
                break;
            }
        }
        XInfoPage *viewController = [[XInfoPage alloc] init];
        [viewController setConfig:dict];
        [self.navigationController pushViewController:viewController animated:YES];
        
        [[self.banner objectAtIndex:index] valueForKey:@"name"];
    }
}

@end
